<!--
 * @Description: 物流下单
 * 
 * @Author: Edan<Edan>
 * @Date: 2024-07-11 09:10:19
 * @FilePath: /yuxiuproweb/src/views/transport/logisticsOrder/index.vue
-->
<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryForm"
      size="small"
      :inline="true"
      v-show="showSearch"
    >
      <el-form-item label="打包单号" prop="packageNumber">
        <el-input
          v-model="queryParams.packageNumber"
          placeholder="请输入打包单号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="订单号" prop="orderId">
        <el-input
          v-model="queryParams.orderId"
          placeholder="请输入订单号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="物流方式" prop="deliveryMethod">
        <el-input
          v-model="queryParams.deliveryMethod"
          placeholder="请输入物流方式"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="客户" prop="clientName">
        <el-input
          v-model="queryParams.clientName"
          placeholder="请输入客户"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="打包日期" prop="pageTimeObj">
        <el-date-picker
          align="center"
          style="width: 240px"
          v-model="pageTimeObj"
          type="daterange"
          range-separator="至"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
          value-format="yyyy-MM-dd"
          format="yyyy-MM-dd"
          @change="handleQuery"
        >
        </el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button
          type="primary"
          icon="el-icon-search"
          size="mini"
          @click="handleQuery"
          >搜索</el-button
        >
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery"
          >重置</el-button
        >
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          >导出</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-edit-outline"
          size="mini"
          @click="handleSetOrder"
          >生成百世物流单</el-button
        >
      </el-col>
      <right-toolbar
        :showSearch.sync="showSearch"
        @queryTable="getList"
      ></right-toolbar>
    </el-row>
    <el-table
      ref="tableList"
      v-loading="loading"
      :data="addressList"
      @selection-change="handleSelectionChange"
      :max-height="maxHeight"
      border
    >
      <el-table-column type="selection" width="55" align="center" />
      <template v-for="(i, index) in tableForm.item">
        <el-table-column
          :key="index"
          :prop="i.itemcode"
          :columnKey="i.itemcode"
          :label="i.itemname"
          :align="i.aligntype ? i.aligntype : 'center'"
          :min-width="i.minwidth"
          :show-overflow-tooltip="i.overflow ? true : false"
          :sortable="i.sortable ? true : false"
          :width="!!i.width ? i.width : ''"
          :fixed="i.fixed"
          v-if="i.displaymark ? true : false"
        >
          <template slot-scope="scope">
            <div v-if="i.itemcode == 'isDefault'">
              <span v-if="scope.row[i.itemcode] == 1">是</span>
              <span v-else>否</span>
            </div>
            <div v-else-if="i.itemcode == 'province'">
              <span>{{ scope.row.province }}</span>
              <span>{{ scope.row.city }}</span>
              <span>{{ scope.row.district }}</span>
            </div>
            <span v-else>{{ scope.row[i.itemcode] }}</span>
          </template>
        </el-table-column>
      </template>
    </el-table>
    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <add-view
      v-if="open.show"
      :show.sync="open.show"
      v-bind="open"
      @update="getList"
    ></add-view>
  </div>
</template>
<script>
import request from "@/utils/request";
import AddView from "./mods/add.vue";
import { tableList } from "./mods/tablecolums.js";
import commonMix from "@/utils/mixins/common";
export default {
  name: "logisticsOrder",
  components: {
    AddView,
  },
  mixins: [commonMix],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 墙布表格数据
      addressList: [],
      // 是否显示弹出层
      open: {
        show: false,
        type: "add",
        data: null,
      },
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 100,
        clientName: null,
        orderId: null,
        packageNumber: null,
        deliveryMethod: null,
      },
      //
      tableForm: tableList,
      selRow: [],
      pageTimeObj: [],
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询高端定制列表 */
    getList() {
      this.loading = true;
      var queryParams = {
        ...this.queryParams,
        startDate: this.pageTimeObj?.[0] || null,
        endDate: this.pageTimeObj?.[1] || null,
      };
      request({
        url: "/mobile/qb/workstation/getPackage",
        method: "get",
        params: queryParams,
      }).then((res) => {
        this.addressList = res.rows;
        this.total = res.total;
        this.loading = false;
      });
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.pageTimeObj = [];
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.packageNumber);
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
      this.selRow = selection;
    },
    // 生成百世物流单
    handleSetOrder() {
      if (!this.selRow.length) {
        this.$message.error("请勾选要生成的物流单");
        return;
      }

      request({
        url: "/md/logistics",
        method: "post",
        data: this.selRow,
      }).then((res) => {
        if (res.code == 200) {
          const datarow = res.data;
          console.log("logistics", datarow);
          this.open = {
            show: true,
            type: "add",
            data: datarow,
          };
        } else {
          this.$message.error(res.msg || "操作失败");
        }
      });
    },
    /** 导出按钮操作 */
    handleExport() {
      if (!this.ids.length) {
        this.$message.error("请勾选要导出的物流单");
        return;
      }
      var idsArr = [];
      for (var i = 0; i < this.ids.length; i++) {
        var item = this.ids[i];
        var obj = {
          packageNumber: item,
        };
        idsArr.push(obj);
      }
      this.downloadExcel(
        "qb/task/export",
        idsArr,
        `物流单_${new Date().getTime()}.xlsx`
      );
      this.$refs.tableList.clearSelection();
    },
  },
};
</script>
